﻿using System;
using System.Web.Mvc;
using Fuse8.ContentManagementFramework.BizFacade;
using Fuse8.ContentManagementFramework.Domain.Special;
using Fuse8.ContentManagementFramework.Web.Extensions;

using Fuse8.ContentManagementFramework.Domain;
using Fuse8.ContentManagementFramework.Web.MvcPowered;

namespace Fuse8.ContentManagementFramework.Web.ApplicationRuntime
{
    public class CmsBaseFrontEndController : CmsBaseApplicationController
    {
        public virtual ActionResult Index()
        {
            var contextPage = this.GetCmsContextPage();

            ActionResult result = null;
            if (contextPage != null)
            {
                switch (contextPage.BehaviourPageType)
                {                    
                    case CmsPageBehaviourTypeKind.Content:
                        result = CmsView(contextPage); 
                        break;
                    case CmsPageBehaviourTypeKind.Placeholder:
                        //todo raise exception or maybe 404?                        
                        break;
                    case CmsPageBehaviourTypeKind.Redirect:
                        var setting = contextPage.RedirectSetting;
                        if (setting != null && CanProcessRedirectPage(setting))
                        {
                            result = new RedirectResult(GetUrl(setting),
                                                        setting.Type == PageRedirectKind.Redirect301);
                        }
                        else
                        {                            
                            //todo raise exception or maybe 404?
                        }
                        break;                    
                }                
            }
            else
            {
                result = Prepare404View();
            }

            return result;                
                
        }        

        private static string GetUrl(CmsRedirectPageSetting setting)
        {
            if (!string.IsNullOrEmpty(setting.UrlToRedirect))
            {
                return setting.UrlToRedirect;
            }

            if (setting.PublicPageIdToRedirect.HasValue)
            {
                return CmsUrlManager.GetUrlByPagePublicId(setting.PublicPageIdToRedirect.Value);
            }

            return CmsUrlManager.PreDefinedfinedPageNotFoundUrl;
        }

        private static bool CanProcessRedirectPage(CmsRedirectPageSetting setting)
        {
            return setting.Type == PageRedirectKind.Redirect301 || setting.Type == PageRedirectKind.Redirect302;
        }

        protected virtual CmsViewResult CmsView(CmsPage contextPage)
        {
            return new CmsViewResult
            {
                TemplateType = contextPage.Template.TemplateType,
                ViewName = contextPage.PageName,
                MasterName = contextPage.Template.TemplateUrl,
                ViewData = ViewData,
                TempData = TempData
            };
        }
    }
}
